using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ExpeditionJourney.HotUpdate;

namespace ExpeditionJourney
{
    public class HotUpdateUI : MonoBehaviour
    {
        [Header("热更新引用")]
        [SerializeField] private HotUpdateManager hotUpdateManager;

        [Header("UI元素")]
        [SerializeField] private Button checkUpdateButton;
        [SerializeField] private Button startUpdateButton;
        [SerializeField] private Text statusText;
        [SerializeField] private Slider progressSlider;
        [SerializeField] private Text progressText;
        [SerializeField] private Text versionText;

        private void Start()
        {
            // 如果没有指定热更新管理器，尝试查找
            if (hotUpdateManager == null)
            {
                hotUpdateManager = FindObjectOfType<HotUpdateManager>();
                if (hotUpdateManager == null)
                {
                    Debug.LogError("找不到HotUpdateManager，请确保场景中存在该组件！");
                    return;
                }
            }

            // 更新版本文本
            UpdateVersionText();

            // 绑定按钮事件
            if (checkUpdateButton != null)
                checkUpdateButton.onClick.AddListener(OnCheckUpdateButtonClicked);

            if (startUpdateButton != null)
            {
                startUpdateButton.onClick.AddListener(OnStartUpdateButtonClicked);
                startUpdateButton.interactable = false; // 初始时不可点击
            }

            // 重置进度条
            if (progressSlider != null)
                progressSlider.value = 0;

            if (progressText != null)
                progressText.text = "0%";

            // 设置状态文本
            if (statusText != null)
                statusText.text = "准备就绪，点击"检查更新"按钮。";

            // 注册热更新事件
            hotUpdateManager.OnCheckVersionComplete += OnCheckVersionComplete;
            hotUpdateManager.OnUpdateProgress += OnUpdateProgress;
            hotUpdateManager.OnUpdateComplete += OnUpdateComplete;
        }

        private void OnDestroy()
        {
            // 取消注册事件，避免内存泄漏
            if (hotUpdateManager != null)
            {
                hotUpdateManager.OnCheckVersionComplete -= OnCheckVersionComplete;
                hotUpdateManager.OnUpdateProgress -= OnUpdateProgress;
                hotUpdateManager.OnUpdateComplete -= OnUpdateComplete;
            }
        }

        private void UpdateVersionText()
        {
            if (versionText != null)
            {
                string currentVersion = PlayerPrefs.GetString("CurrentVersion", "v1.0");
                versionText.text = $"当前版本: {currentVersion}";
            }
        }

        private void OnCheckUpdateButtonClicked()
        {
            if (statusText != null)
                statusText.text = "正在检查更新...";

            if (checkUpdateButton != null)
                checkUpdateButton.interactable = false;

            if (startUpdateButton != null)
                startUpdateButton.interactable = false;

            // 开始检查更新
            hotUpdateManager.CheckForUpdates();
        }

        private void OnStartUpdateButtonClicked()
        {
            if (statusText != null)
                statusText.text = "开始更新资源...";

            if (checkUpdateButton != null)
                checkUpdateButton.interactable = false;

            if (startUpdateButton != null)
                startUpdateButton.interactable = false;

            // 开始更新
            hotUpdateManager.StartUpdate();
        }

        private void OnCheckVersionComplete(bool hasUpdate, string message)
        {
            // 更新UI状态
            if (statusText != null)
                statusText.text = message;

            if (checkUpdateButton != null)
                checkUpdateButton.interactable = true;

            if (startUpdateButton != null)
                startUpdateButton.interactable = hasUpdate;
        }

        private void OnUpdateProgress(float progress, string resourceName)
        {
            // 更新进度条
            if (progressSlider != null)
                progressSlider.value = progress;

            // 更新进度文本
            if (progressText != null)
                progressText.text = $"{(int)(progress * 100)}%";

            // 更新状态文本
            if (statusText != null)
                statusText.text = $"正在更新: {resourceName}";
        }

        private void OnUpdateComplete(bool success, string message)
        {
            // 更新UI状态
            if (statusText != null)
                statusText.text = message;

            if (checkUpdateButton != null)
                checkUpdateButton.interactable = true;

            if (startUpdateButton != null)
                startUpdateButton.interactable = false;

            // 更新版本文本
            UpdateVersionText();

            // 更新完成后可以在这里添加重启应用或重新加载场景的逻辑
            if (success)
            {
                Debug.Log("热更新成功，可以在这里添加重新加载资源或场景的代码");
            }
        }
    }
} 